package com.tinama.jedis01.demo.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * @author lgc
 * @createDate 2021/1/14 - 10:39
 */
public class InvocationImpl implements InvocationHandler {
    /**
     * 代理类中的真实对象
     */
    private final Object target;
    public InvocationImpl(Object target){
       this.target = target;
    }


    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Object invoke = null;
        if (target instanceof SendSms){
            System.out.println("属于短信对象before method " + method.getName());
            if ("send".equals(method.getName())){
                String arg =(String) args[0];
                arg = arg + "(参数增强)";
                method.invoke(target,arg);
            }
            method.invoke(target, args);
            System.out.println("after method " + method.getName());
        }

        if (target instanceof OPenPhone){
            System.out.println("属于手机before method " + method.getName());
            if ("open".equals(method.getName())){
                String arg =(String) args[0];
                invoke = method.invoke(target,arg);
                String invoke1 = (String) invoke;
                String[] split = invoke1.split("：");
                String s = "13" + split[1];
                System.out.println("phone method (返回值增强)");
                return split[0] + s;
            }
             method.invoke(target, args);
            System.out.println("after method " + method.getName());
        }

        return invoke;
    }
}
